var dhjl_app = function() {
	var path = Constants.amUrlPath;
	var ractive;
    var param_ywy = "000000";
	function renderView() {

        
		ractive = new Ractive({
			el : ".main-contexts",
			template : "#main-template",
			onrender: function() {
				//manager.loadProperties(this, "dhjl");
				//manager.loadProperties(this, "error");
			},
			oncomplete : function() {
				var ywy_ = getQueryString("ywy");
				if(ywy_){
					param_ywy = ywy_;
				}
				var _sc_param = {
						pageSize : Constants.page.pagesize,
						currPage : 1,
						ywy : param_ywy
				};
				initTable(_sc_param,false);
				
				var quanbu = "全部";
	            $.get(manager.getContextPath() + "common/getCodeListByCodeClass?codeClass=DHZT", function (data) {
	                //增加全部的提示
	                data.unshift({"code": -1, "codedesc": quanbu});
	                store.set("DHZT", JSON.stringify(data));
	                selecttextInfo();
	                setWDCode("dhjl_DHZT", -1);
	            });
	            
			}
		});

		ractive.on({
			search: function () {
                loadtable();
            },
            dhfh : function(e) {
            	dhfh(e);
            },
			ckdz : function(e) {
				ckdz(e);
			}
		});

	}

	function review() {
		var _param = {};
		if (typeof ractive != "undefined") {
			var param = ractive.get("param_dhjl");
			param.lpmc = $.trim(param.lpmc);
			param.jfqs = $.trim(param.jfqs);
			param.jfjs = $.trim(param.jfjs);
			param.dhrmc = $.trim(param.dhrmc);
			_param.lpmc = param.lpmc;
			_param.jfqs = param.jfqs;
			_param.jfjs = param.jfjs;
			_param.dhrmc = param.dhrmc;
			_param.dhsjqs = param.dhsjqs ? (param.dhsjqs.length == 10 ? param.dhsjqs + "T00:00:00+08" : param.dhsjqs) : "";
			_param.dhsjjs = param.dhsjjs ? (param.dhsjjs.length == 10 ? param.dhsjjs + "T23:59:59+08" : param.dhsjjs) : "";
			_param.zt = getWDCode("dhjl_DHZT");
			_param.ywy = param_ywy;
			ractive.set("param_dhjl", param);
		}
		return _param;
	}

	function initTable(param,isSearch) {
		var _title_lpmc = "礼品名称";
		var _title_id = "兑换编号";
		var _title_jf = "积分";
		var _title_dhrmc = "用户名称";
		var _title_dhsj = "兑换时间";
		var _title_zt = "状态";

		var _title_cz = "操作";
		var _btn_fh = "发货";
		var _btn_ckdz = "查看地址";
		var _btn_ckwl = "查看物流";
		
		var _table_title = [_title_lpmc, _title_id, _title_jf, _title_dhrmc, _title_dhsj, _title_zt, _title_cz];
		var _col_model = [
  				{
  					name : 'lpmc',
  					width : 100,
					float:'left'/*,
			        innerhtml: function (opts) {
			            if (opts.lpmc) {
			                return '<p class="btn-lpmc">' + opts.lpmc + '</p>';
			            } else {
			                return "";
			            }
			        }*/
  				},
				{
					name : 'id',
					width : 100
				},
				{
					name : 'jf',
					width : 40
				},
				{
					name : 'dhrmc',
					width : 60,
					float:'left'
				},
				{
					name : 'dhsj',
					width : 150,
			        innerhtml: function (opts) {
			            if (opts.dhsj) {
			                return moment(opts.dhsj).format("YYYY-MM-DD HH:mm:ss");
			            } else {
			                return "";
			            }
			        }
				},
				{
					name : 'zt',
					width : 40,
					innerhtml:function(opts){
						var str = opts.ztDesc;
						if(opts.zt == Constants.code.DHZT.DHZT_WFF){
							str = "<font class='ad-font-orange'>" + opts.ztDesc + "</font>"
						}
						return str;
					}
				},
				{
					name : 'sz',
					width : 60,
					innerhtml:function(opts){
							var str="";
							if(opts.shfs != "01"){
								str += "<a href='javascript:void(0);' class='ad-marginleft6' dhid='"+opts.id+"' onclick='dhjl_app.ckdz(this)' title=''><font class='ad-font-blue'>" + _btn_ckdz +"</font></a><br/>";
							}else{
								str += "<a href='javascript:void(0);' class='ad-marginleft6' dhid='"+opts.id+"' onclick='dhjl_app.ckwl(this)' title=''><font class='ad-font-blue'>" + _btn_ckwl +"</font></a><br/>";
							}
							if(opts.ddlb == Constants.code.DHLB.DHLB_LP && opts.zt == Constants.code.DHZT.DHZT_WFF){
								str += "<a href='javascript:void(0);' class='ad-marginleft6' dhid='"+opts.id+"' onclick='dhjl_app.dhfh(this)' title=''><font class='ad-font-orange'>" + _btn_fh +"</font></a>";
							}
							return str;
					}
				}
				];
		jQuery("#wd-table").wdGrid({
			url:path+'dhgl/getDhjlPagination',
			colNames : _table_title,
			colModel : _col_model,
			dataType : "GET",
			submitType : "noForm",
			postData : param,
			pager : "#wd-pager",
	       // checkflg: true,
	        checktext:["id"],
			rowList: Constants.page.rowList,
	        pagesize: Constants.page.pagesize
		});
	}
    
    //加载table
    function loadtable(){
    	//重新加载table
		var _cx_param = {
				pageSize : Constants.page.pagesize,
				currPage : 1
		};
		var _param = review();
		// 合并查询参数
		var objs = $.extend(_cx_param, _param);
		initTable(objs,true);
		
    }
    
    function dhfh(e){
		var ddid = $(e).attr("dhid");
		
		var fhDialog = {
			url: "../zyddgl/ddgl_fh.html",
			title: "发货",
			width: "440",
			height: "220",
			successCallBackFun: function(){
				var shfs = fhWin.Win.document.getElementById("shfs").value;
				var fhParam = {
					dhid: ddid,
					shfs: shfs,
					hydh: fhWin.Win.document.getElementById("hydh").value,
					ywy : param_ywy
				};

				var cyfbh = fhWin.Win.document.getElementById("CYF").value;
				if(shfs == "01"){
					if(!cyfbh){
						alert("承运方不能为空！");
						return;
					}else if(cyfbh == "-1"){
						fhParam.cyfmc = fhWin.Win.document.getElementById("qtcyf").value;
					}else{
						fhParam.cyfmc = fhWin.Win.document.getElementById("fh_CYF").value;
						fhParam.cyfbh = fhWin.Win.document.getElementById("CYF").value;
					}
					if(!fhParam.cyfmc){
						alert($.i18n.prop("承运方不能为空！"));
						return;
					}
					if(!fhParam.hydh){
						alert("货运单号不能为空！");
						return;
					}
				}
				$.ajax({
					url: manager.getContextPath() + "zyddgl/ddfh",
					type: "post",
					data: JSON.stringify(fhParam),
					contentType: "application/json",
					dataType: "json",
					beforeSend: function(){
						//manager.block();
					},
					complete: function(){
						//manager.unblock();
					},
					success: function(data){
						if(data.result){//发货成功
							function fn() {
								wdDialogClosed("dialog");
								loadtable();
							}
							var objs = {txt:"发货成功！",callBackFun:fn}
							success(objs);
						}else{//发货失败
							manager.errorMsg(data, null);
						}
					},
					error: function(){
						//manager.unblock();
					}
				})
			},
			cancelCallBackFun: function(){
				
			}
		}
		
		var fhWin = dialog(fhDialog);
    }
    
    function ckdz(e){
    	var dhid = $(e).attr("dhid");
    	
    	var ckdzDialog = {
    			url: "dhjl_ckdz.html?dhid=" + dhid,
    			title: "地址",
    			width: "550",
    			height: "200",
    			successCallBackFun: function(){
					wdDialogClosed("dialog");
    			},
    			cancelCallBackFun: function(){
    				
    			}
    	}
    	
    	var fhWin = dialog(ckdzDialog);
    }
    //查看物流
    function ckwl(e){
    	var dhid = $(e).attr("dhid");
    	var wlgzDialog = {
    			url: "../wlgz/wlgz.html?dhid="+dhid+'&flag=1',
    			title: '物流跟踪',
    			width: "630",
    			height: "400",
    			successCallBackFun: function(){
    				wdDialogClosed("dialog");
    			}
    		}
    		var wlgzWin = dialog(wlgzDialog);
    }
	/* 获取地址栏传递的参数 */
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }
    
	return {
		init : function() {
			renderView();
		},
		dhfh : function(e) {
			dhfh(e);
		},
		ckdz : function(e) {
			ckdz(e);
		},
		ckwl:function(e){
			ckwl(e);
		}
	};
}();
$(document).ready(dhjl_app.init);